﻿using System.Collections.Generic;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Data;
using System.Linq;

namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {

        /// <summary>
        /// GetJob is used to collect the tasks to finish the order creation
        /// </summary>
        /// <param name="FInterID"></param>
        /// <returns></returns>
        public void SJYBWB13(DataSet db)
        {
            try
            { 
                DataTable tbWBBST = db.Tables["ICC_STOCK"];
                DataTable dtOutput = new DataTable();
                int idx = 1;
                int x = 6;
                #region======Title=========== 
                //dtOutput.Columns.Add("TIMERANGE", typeof(System.String));
                exAgent.SetCellValue(x, idx++, "车间"); dtOutput.Columns.Add("AMLB", typeof(System.String));
                exAgent.SetCellValue(x, idx++, "班别"); dtOutput.Columns.Add("WBBREFSCHL", typeof(System.String));
                exAgent.SetCellValue(x, idx++, "织物名称"); dtOutput.Columns.Add("WABEZ", typeof(System.String));
                exAgent.SetCellValue(x, idx++, "批号"); dtOutput.Columns.Add("ABPARTEX", typeof(System.String));
                exAgent.SetCellValue(x, idx++, "织物编号"); dtOutput.Columns.Add("WAART", typeof(System.String));
                exAgent.SetCellValue(x, idx++, "机台号"); dtOutput.Columns.Add("LOOMID", typeof(System.String));
                exAgent.SetCellValue(x, idx++, "米数"); dtOutput.Columns.Add("WBEWMTR", typeof(System.String));
                exAgent.SetCellValue(x, idx++, "卷数"); dtOutput.Columns.Add("PCS", typeof(System.String)); 
                #endregion
                dtOutput.Merge(tbWBBST, false, MissingSchemaAction.Ignore);
                string title = "",room=" ";
                if (tbWBBST.Rows.Count!=0)
                {
                    title = "验布产量" + tbWBBST.Rows[0]["TIMERANGE"].ToString(); 
                }
                exAgent.SetCellValue(1, 1, title); 
                foreach (DataRow row in dtOutput.Rows)
                {                                 
                    row["WAART"] = Helpers.Helper.GetGB2312Value(row["WAART"].ToString().Trim());
                    row["WABEZ"] = Helpers.Helper.GetGB2312Value(row["WABEZ"].ToString().Trim());
                    row["ABPARTEX"] = Helpers.Helper.GetGB2312Value(row["ABPARTEX"].ToString().Trim());
                    room = row["LOOMID"].ToString().Substring(0, 1);
                    row["AMLB"] = room+"车间";
                    room = " ";
                    if (row["WBBREFSCHL"].ToString().Trim()=="1")
                    {
                        row["WBBREFSCHL"] = "甲班";
                    }
                    if (row["WBBREFSCHL"].ToString().Trim() == "2")
                    {
                        row["WBBREFSCHL"] = "乙班";
                    }
                }
                var qGW = from MUrow in tbWBBST.AsEnumerable()
                          group MUrow by new
                          {
                              ART = MUrow["WAART"].ToString(),
                              DES = MUrow["WABEZ"].ToString(),
                              CHEJ = MUrow["LOOMID"].ToString().Substring(0, 1),
                          }
                              into g
                              select new
                              {
                                  KEY = g.Key,
                                  PCS = g.Sum(XD => Convert.ToDouble(XD["PCS"].ToString())),
                              };
                x=7;
                exAgent.SetCurrentSheet("SJYBWB13");
                exAgent.CopySheet(1 + "车间");
                foreach (var GW in qGW)
                {
                   
                    string art = GW.KEY.ART;
                    string des = GW.KEY.DES;
                    string chej = GW.KEY.CHEJ;
                    if (chej == "2") continue;
                   
                    exAgent.SetCellValue(x, 1, (Helpers.Helper.GetGB2312Value(des)).Trim()+(Helpers.Helper.GetGB2312Value(art)).Trim());
                    x++;
                    DataRow[] rows = dtOutput.Select("WAART='" + art + "' AND LOOMID like '"+chej+"%'");
                    int count = rows.Length;
                    exAgent.RangeCopy(5003, 5003, x, x + count-1, true);

                    DataTable dt = rows.CopyToDataTable();
                    exAgent.DataTableToExcel(dt, x, 1);
                    x = x + count;  
                    exAgent.RangeCopy(4, 4, x, x, true);
                    exAgent.SetCellValue(x, 1, "小计:");
                    exAgent.SetCellValue(x, 8, GW.PCS+"卷");
                    x++;
                    x++;                   
                }
                x = 7;
                exAgent.SetCurrentSheet("SJYBWB13");
                exAgent.CopySheet(2 + "车间");
                foreach (var GW in qGW)
                {

                    string art = GW.KEY.ART;
                    string des = GW.KEY.DES;
                    string chej = GW.KEY.CHEJ;
                    if (chej == "1") continue;  
                    exAgent.SetCellValue(x, 1, (Helpers.Helper.GetGB2312Value(des)).Trim() + (Helpers.Helper.GetGB2312Value(art)).Trim());
                    x++;
                    DataRow[] rows = dtOutput.Select("WAART='" + art + "' AND LOOMID like '" + chej + "%'");
                    int count = rows.Length;
                    exAgent.RangeCopy(5003, 5003, x, x + count - 1, true);

                    DataTable dt = rows.CopyToDataTable();
                    exAgent.DataTableToExcel(dt, x, 1);
                    x = x + count;
                    exAgent.RangeCopy(4, 4, x, x, true);
                    exAgent.SetCellValue(x, 1, "小计:");
                    exAgent.SetCellValue(x, 8, GW.PCS + "卷");
                    x++;
                    x++;
                }
                exAgent.DeleteSheet("SJYBWB13");
                exAgent.SetCurrentSheet(1);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

    }
}
